package com.wellsoft.mobileoa.utils;

import java.util.ArrayList;
import java.util.List;

import com.wellsoft.mobileoa.utils.action.CancelAction;
import com.wellsoft.mobileoa.utils.action.CheckValidAction;
import com.wellsoft.mobileoa.utils.action.ExitAction;
import com.wellsoft.mobileoa.utils.action.GoBackAction;
import com.wellsoft.mobileoa.utils.action.HttpGetAciton;
import com.wellsoft.mobileoa.utils.action.HttpPostAction;
import com.wellsoft.mobileoa.utils.action.HttpPostWithFieldAction;
import com.wellsoft.mobileoa.utils.action.LogoutAction;
import com.wellsoft.mobileoa.utils.action.PopMultiSelectAction;
import com.wellsoft.mobileoa.utils.action.PopNewPageAction;
import com.wellsoft.mobileoa.utils.action.PopSingleSelectAction;
import com.wellsoft.mobileoa.utils.action.PutForwardOpinionAction;
import com.wellsoft.mobileoa.utils.action.ResetAction;
import com.wellsoft.mobileoa.utils.action.SelectAllAction;
import com.wellsoft.mobileoa.utils.action.SyncAddress;
import com.wellsoft.mobileoa.utils.action.SyncAppointment;
import com.wellsoft.mobileoa.wsmodel.WSOA_Action;
import com.wellsoft.mobileoa.wsmodel.WSOA_PostField;
import com.wmobile.utils.ServiceResponse;

public abstract class BaseAction {
	protected WSOA_Action action;
	protected BaseView baseView;
	protected int actionNo;
	protected Object initData;
	protected boolean isMainThread = false;

	public void setMainThread(boolean isMainThread) {
		this.isMainThread = isMainThread;
	}

	public int getActionNo() {
		return actionNo;
	}

	public void setActionNo(int actionNo) {
		this.actionNo = actionNo;
	}

	public static BaseAction createActionFactory(WSOA_Action action,
			BaseView baseView) {
		if (action == null
				|| SystemConfig.stringIsNullOrEmpty(action.getFuncType()))
			return null;
		if (action.getFuncType().equals("0")) {
			return new HttpGetAciton(action, baseView);
		} else if (action.getFuncType().equals("1")) {
			return new HttpPostAction(action, baseView);
		} else if (action.getFuncType().equals("2")) {
			return new HttpPostWithFieldAction(action, baseView);
		} else if (action.getFuncType().equals("3")) {
			return new HttpPostAction(action, baseView);
		} else if (action.getFuncType().equals("4")) {
			return new GoBackAction(action, baseView);
		} else if (action.getFuncType().equals("5")) {
			return new HttpPostAction(action, baseView);
		} else if (action.getFuncType().equals("7")) {

		} else if (action.getFuncType().equals("8")) {
			return new SelectAllAction(action, baseView);
		} else if (action.getFuncType().equals("9")) {
			return new PopNewPageAction(action, baseView);
		} else if (action.getFuncType().equals("v")) {
			return new CheckValidAction(action, baseView);
		} else if (action.getFuncType().equals("r")) {
			return new ResetAction(action, baseView);
		} else if (action.getFuncType().equals("u")) {
			return new GoBackAction(action, baseView);
		} else if (action.getFuncType().equals("o")) {
			return new LogoutAction(action, baseView);
		} else if (action.getFuncType().equals("q")) {
			return new ExitAction(action, baseView);
		} else if (action.getFuncType().equals("e")) {

		} else if (action.getFuncType().equals("f")) {
			return new PutForwardOpinionAction(action, baseView);
		} else if (action.getFuncType().equals("g")) {
			return new PopSingleSelectAction(action, baseView);
		} else if (action.getFuncType().equals("h")) {
			return new PopMultiSelectAction(action, baseView);
		} else if (action.getFuncType().substring(0, 1).equals("z")) {
			if (action.getFuncType().indexOf("address") > -1) {
				return new SyncAddress(action, baseView);
			} else if (action.getFuncType().indexOf("appointment") > -1) {
				return new SyncAppointment(action, baseView);
			}
		}
		return null;
	}

	public ServiceResponse excuteAction() {
		return new ServiceResponse(true);
	}

	public static ServiceResponse executeActions(String actionIds,
			WSOA_Action exeAction, BaseView actionView, boolean isMainThread) {
		return executeActions(actionIds, exeAction, actionView, actionView
				.getPage().getActions(), null, isMainThread);
	}

	public static ServiceResponse executeActions(String actionIds,
			WSOA_Action exeAction, BaseView actionView) {
		return executeActions(actionIds, exeAction, actionView, actionView
				.getPage().getActions(), null, false);
	}

	public static ServiceResponse executeActions(String actionIds,
			WSOA_Action exeAction, BaseView actionView, String selectedValue) {
		return executeActions(actionIds, exeAction, actionView,
				actionView.page.getActions(), selectedValue, false);
	}

	public static ServiceResponse executeActions(String actionIds,
			WSOA_Action exeAction, BaseView actionView,
			List<WSOA_Action> withInActionList, String selectedValue,
			boolean isMainThread) {
		if (exeAction == null
				|| SystemConfig.stringIsNullOrEmpty(exeAction.getFuncType())) {
			List<WSOA_Action> actionLists = getActionById(actionIds,
					withInActionList, selectedValue);
			if (actionLists == null || actionLists.size() == 0) {
				return new ServiceResponse(false, "找不到动作");
			}
			for (WSOA_Action wsAction : actionLists) {
				BaseAction action = BaseAction.createActionFactory(wsAction,
						actionView);
				if (action == null) {
					return new ServiceResponse(false, "找不到动作"
							+ wsAction.getActionID());
				}
				action.setMainThread(isMainThread);
				ServiceResponse response = action.excuteAction();
				if (!response.getExeFlag()) {
					return response;
				}
			}
		} else {
			BaseAction action = BaseAction.createActionFactory(exeAction,
					actionView);
			action.setMainThread(isMainThread);
			ServiceResponse response = action.excuteAction();
			if (!response.getExeFlag()) {
				return response;
			}
		}
		return new ServiceResponse(true);
	}

	public static List<WSOA_Action> getActionById(String actionIds,
			List<WSOA_Action> withInActionList, String selectedValue) {
		List<WSOA_Action> actionList = new ArrayList<WSOA_Action>();
		if (actionIds.indexOf(";") > -1) {
			String[] actionArr = actionIds.split(";");
			for (String subActionID : actionArr) {
				if (SystemConfig.stringIsNullOrEmpty(subActionID)) {
					continue;
				}
				if (subActionID.indexOf("|") > -1) {
					if (!SystemConfig.stringIsNullOrEmpty(selectedValue)) {
						String[] optionArr = subActionID.split("|");
						if (selectedValue.equals(optionArr[0])) {
							for (WSOA_Action subAction : withInActionList) {
								if (subAction.getActionID()
										.equals(optionArr[1])) {
									actionList.add(subAction);
									break;
								}
							}
						}
					}

				} else {
					for (WSOA_Action subAction : withInActionList) {
						if (subAction.getActionID().equals(subActionID)) {
							actionList.add(subAction);
							break;
						}
					}
				}
			}
		} else {
			for (WSOA_Action subAction : withInActionList) {
				if (subAction.getActionID().equals(actionIds)) {
					actionList.add(subAction);
					break;
				}
			}
		}
		return actionList;
	}
}
